﻿using GatewayClient.MQ;
using Iot.Device.CharacterLcd;
using IotDeviceToolHepler.Device;
using IotDeviceToolHepler.Model.device;
using IotDeviceToolHepler.Utils;
using Models.Common;
using Models.Constant;
using Models.Device;
using Models.TaskPlain;
using Newtonsoft.Json;

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using static Models.Common.EnumDeviceState;

namespace GatewayClient.Device
{
    public class ServiceDeviceDisplay
    {
        /// <summary>
        /// 1602显示器
        /// </summary>
        /// <param name="commandLogModel"></param>
        public void executeDisplay1602(MQReqModel<object> mQReqModel)
        {
            try
            {
                ModelWriteLcd1602 modelWriteLcd1602 = new ModelWriteLcd1602();
                List<ModelWriteLcd1602> modelWriteLcd1602s = new List<ModelWriteLcd1602>();


                CommandLogModel commandLogModel = (CommandLogModel)mQReqModel.data;
                ModelCommandAgreement modelCommandAgreement = JsonConvert.DeserializeObject<ModelCommandAgreement>(commandLogModel.content);

                //if(modelCommandAgreement.agreementType== "i2c")
                //{
                ModelCommandAgreementParameter modelI2C = modelCommandAgreement.agreementParameter;
                // }
                List<ModelLcd1602DisplayContent> modelLcd1602DisplayContents = (List<ModelLcd1602DisplayContent>)modelCommandAgreement.content;
                //List<ModelLcd1602DisplayContent> modelLcd1602DisplayContents = JsonConvert.DeserializeObject<List<ModelLcd1602DisplayContent>>(mQReqModel.data.content);
                foreach (ModelLcd1602DisplayContent item in modelLcd1602DisplayContents)
                {
                    modelWriteLcd1602 = new ModelWriteLcd1602();
                    modelWriteLcd1602.content = item.displayContent;
                    modelWriteLcd1602.row = item.row;
                    modelWriteLcd1602.left = item.left;
                    modelWriteLcd1602s.Add(modelWriteLcd1602);
                }

                //DeviceDisplayLcd1602.Write(modelLcd1602DisplayContents[0].busId, modelWriteLcd1602s);

                DeviceDisplay_Lcd1602.Write(modelI2C.busId, modelWriteLcd1602s);

                //返回执行状态
                //MQReqModel<CommandLogModel> mQReqModel = new MQReqModel<CommandLogModel>();
                //mQReqModel.respEvent = MQEventEnum.Event_Display;
                mQReqModel.respCode = ResponseCode.SUCCESS;
                mQReqModel.respMsg = ResponseCode.SUCCESS_MSG;
                //mQReqModel.gatewayDeviceId = UtilsMachineInfo.getMachineId();
                // mQReqModel.data = commandLogModel;
                //mQReqModel.guid = System.Guid.NewGuid().ToString();
                new ServiceMQ_Helper().sendMsg(mQReqModel);
            }
            catch (Exception ex)
            {
                //返回执行状态
                // MQReqModel<CommandLogModel> mQReqModel = new MQReqModel<CommandLogModel>();
                // mQReqModel.respEvent = MQEventEnum.Event_Display;
                mQReqModel.respCode = ResponseCode.System_ERROR;
                mQReqModel.respMsg = ResponseCode.System_ERROR_MSG;
                // mQReqModel.gatewayDeviceId = UtilsMachineInfo.getMachineId();
                // mQReqModel.data = commandLogModel;
                // mQReqModel.guid = System.Guid.NewGuid().ToString();
                new ServiceMQ_Helper().sendMsg(mQReqModel);
            }

        }

       
    }
}
